﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using Fodee.BusinessComponent;
using Fodee.DataEntity;
using Fodee.Common;

namespace FodeeAdmin.SystemSettings
{
    public partial class DepartmentForm : BaseSystemSettingsForm
    {
        private fodDepartmentBC bc = new fodDepartmentBC();

        //private FodeeDataSet FodeeDS;
        //public FodeeDataSet FodeeDS
        //{
        //    get
        //    {
        //        return FodeeDS;
        //    }
        //    set
        //    {
        //        FodeeDS = value;
        //    }
        //}

        //private int keyID;
        //public int KeyID
        //{
        //    get { return keyID; }
        //    set { keyID = value; }
        //}

        public DepartmentForm()
        {
            InitializeComponent();

            this.grid.AutoGenerateColumns = false;
        }

        public void InitForm()
        {
            //foreach (Control c in this.Controls)
            //{
            //    if (c is ValidationTextBox.ValidationTextBox && ((ValidationTextBox.ValidationTextBox)c).Required)
            //    { 
            //        c.Name.Substring(3)
            //    }
            //}
        }

        public void BindData()
        {
            FodeeDS = bc.GetAll();
            this.grid.DataSource = FodeeDS.fod_Department.DefaultView;
            this.grid.ClearSelection();
            ClearForm();
        }

        public override void ClearForm()
        {
            base.ClearForm();
            this.grid.ClearSelection();
            SetOperation(Operation.Add);
        }

        public void AddData()
        {
            FodeeDataSet ds = new FodeeDataSet();
            FodeeDataSet.fod_DepartmentRow row = ds.fod_Department.Newfod_DepartmentRow();
            row.DepartmentNo = this.tbxDepartmentNo.Text;
            row.DepartmentName = this.tbxDepartmentName.Text;
            row.DepartmentRemarks = this.tbxDepartmentRemarks.Text;
            Util.SetCommonFields(row);
            ds.fod_Department.Addfod_DepartmentRow(row);
            int result = bc.Add(ds);
            if (result>0)
            {
                MessageBox.Show("添加成功");
                BindData();
            }
            else
            {
                MessageBox.Show("添加失败");
            }
        }

        public void UpdateData()
        {
            FodeeDataSet.fod_DepartmentRow row = FodeeDS.fod_Department.FindByID(KeyID);
            row.DepartmentNo = this.tbxDepartmentNo.Text;
            row.DepartmentName = this.tbxDepartmentName.Text;
            row.DepartmentRemarks = this.tbxDepartmentRemarks.Text;
            bool result = bc.Update(FodeeDS);
            if (result)
            {
                MessageBox.Show("修改成功");
                BindData();
            }
            else
            {
                MessageBox.Show("修改失败");
            }
        }

        public void FillData()
        {
            FodeeDataSet.fod_DepartmentRow row = FodeeDS.fod_Department.FindByID(KeyID);
            this.tbxDepartmentNo.Text = row.DepartmentNo;
            this.tbxDepartmentName.Text = row.DepartmentName;
            this.tbxDepartmentRemarks.Text = row.DepartmentRemarks;

            SetOperation(Operation.Edit);

            if (!base.ValidateForm())
            {
                return;
            }
        }

        public void DeleteData()
        {
            //FodeeDataSet.fod_DepartmentRow row = FodeeDS.fod_Department.FindByID(KeyID);
            //row.Delete();
            bool result = bc.Delete(KeyID);
            if (result)
            {
                MessageBox.Show("删除成功");
                BindData();
            }
            else
            {
                MessageBox.Show("删除失败");
            }
        }

        public void SetOperation(Operation operation)
        {
            if (operation == Operation.Add)
            {
                this.btnSave.Text = "添加";
            }
            else
            {
                this.btnSave.Text = "修改";
            }

            FormOperation = operation;
        }

        //public override bool ValidateForm()
        //{
        //    int inValidCount = 0;
        //    foreach (Control c in this.Controls)
        //    {
        //        if (c is CustomComponent.RequiredFieldValidator)
        //        {
        //            CustomComponent.RequiredFieldValidator r = c as CustomComponent.RequiredFieldValidator;
        //            r.Validate();
        //            if (!r.IsValid)
        //            {
        //                inValidCount++;
        //            }
        //        }
        //    }

        //    return inValidCount==0;
        //}

        private void DepartmentForm_Load(object sender, EventArgs e)
        {
            BindData();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!base.ValidateForm())
            {
                return;
            }

            if (FormOperation == Operation.Edit)
            {
                UpdateData();
            }
            else
            {
                AddData();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            ClearForm();
        }


        private void grid_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0 || e.ColumnIndex < 0)
            {
                return;
            }

            KeyID = int.Parse(this.grid.Rows[e.RowIndex].Cells["ID"].Value.ToString());
            string action = this.grid.Columns[e.ColumnIndex].Name;
            if (action.ToLower() == "delete")
            {
                bool isDelete = MessageBox.Show("确定删除吗?", "删除提示", MessageBoxButtons.OKCancel) == DialogResult.OK;
                if (isDelete)
                {
                    DeleteData();
                }
            }
            else
            {
                FillData();
            }
        }
    }
}
